package com.translator.util;

import com.translator.config.AppConfig;

import java.net.Authenticator;
 import java.net.PasswordAuthentication;

/**
 * 网络代理管理工具类
 * 负责管理和应用网络代理设置
 */
public class ProxyManager {
    
    private static ProxyManager instance;
    private boolean proxyApplied = false;
    
    private ProxyManager() {
        // 私有构造函数
    }
    
    /**
     * 获取单例实例
     * 
     * @return ProxyManager实例
     */
    public static synchronized ProxyManager getInstance() {
        if (instance == null) {
            instance = new ProxyManager();
        }
        return instance;
    }
    
    /**
     * 应用代理设置
     * 根据配置设置系统代理
     */
    public void applyProxySettings() {
        AppConfig config = AppConfig.getInstance();
        
        if (config.isProxyEnabled()) {
            // 设置HTTP代理
            System.setProperty("http.proxyHost", config.getProxyHost());
            System.setProperty("http.proxyPort", String.valueOf(config.getProxyPort()));
            
            // 设置HTTPS代理
            System.setProperty("https.proxyHost", config.getProxyHost());
            System.setProperty("https.proxyPort", String.valueOf(config.getProxyPort()));
            
            // 设置代理认证
            if (!config.getProxyUsername().isEmpty() && !config.getProxyPassword().isEmpty()) {
                System.setProperty("http.proxyUser", config.getProxyUsername());
                System.setProperty("http.proxyPassword", config.getProxyPassword());
                System.setProperty("https.proxyUser", config.getProxyUsername());
                System.setProperty("https.proxyPassword", config.getProxyPassword());
                
                // 设置Authenticator
                Authenticator.setDefault(new Authenticator() {
                    @Override
                    protected PasswordAuthentication getPasswordAuthentication() {
                        return new PasswordAuthentication(
                                config.getProxyUsername(), 
                                config.getProxyPassword().toCharArray());
                    }
                });
            }
            
            proxyApplied = true;
        } else if (proxyApplied) {
            // 清除代理设置
            clearProxySettings();
        }
    }
    
    /**
     * 清除代理设置
     */
    public void clearProxySettings() {
        // 清除HTTP代理
        System.clearProperty("http.proxyHost");
        System.clearProperty("http.proxyPort");
        System.clearProperty("http.proxyUser");
        System.clearProperty("http.proxyPassword");
        
        // 清除HTTPS代理
        System.clearProperty("https.proxyHost");
        System.clearProperty("https.proxyPort");
        System.clearProperty("https.proxyUser");
        System.clearProperty("https.proxyPassword");
        
        // 重置Authenticator
        Authenticator.setDefault(null);
        
        proxyApplied = false;
    }
    
    /**
     * 检查代理是否已应用
     * 
     * @return 代理是否已应用
     */
    public boolean isProxyApplied() {
        return proxyApplied;
    }
}